package com.itboye.mengwu.app.ui.mymeng.personsettings;

import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.AddressQueryModel;
import com.itboye.mengwu.data.presenter.AddressQueryPresenter;

public class DetailLocationActivity extends BaseActivity implements
		OnClickListener, Observer {

	private RelativeLayout detailrela;
	private RelativeLayout namerela;
	private RelativeLayout phonerela;
	private RelativeLayout ziprela;
	private RelativeLayout backrela;
	private RelativeLayout deleterela;
	private RelativeLayout saverela;
	private RelativeLayout defaultrela;

	private RelativeLayout defaultrela1;

	private TextView provincetext;
	private EditText detailtext;
	private EditText nametext;

	boolean status = false;

	private ImageView defaultbutton;

	private EditText phonetext;
	private EditText ziptext;
	private String id;
	private int defaultid;
	private String uid;
	AddressQueryPresenter addressQueryPresenter;
	ImageView back;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_detaillocation;
	}

	@Override
	public void onMyCreate() {
		addressQueryPresenter = new AddressQueryPresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		initview();
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@SuppressLint("NewApi")
	public void initview() {
		Intent intent = getIntent();
		String wholelocation = intent.getStringExtra("wholelocation");
		String detail = intent.getStringExtra("detail");
		String name = intent.getStringExtra("name");
		String phone = intent.getStringExtra("phone");
		String zipcode = intent.getStringExtra("zipcode");
		defaultid = intent.getIntExtra("isDefault", 0);
		int isDefault = intent.getIntExtra("isDefault", 0);
		if (isDefault == 1) {
			defaultbutton.setImageDrawable(getResources().getDrawable(
					R.drawable.default_on));
			status = true;
		} else {
			defaultbutton.setImageDrawable(getResources().getDrawable(
					R.drawable.default_off));
			status = false;
		}
		id = intent.getStringExtra("id");
		provincetext.setText(wholelocation);
		detailtext.setText(detail);
		nametext.setText(name);
		phonetext.setText(phone);
		ziptext.setText(zipcode);

	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.detailrela:

			break;
		case R.id.backrela:
			this.finish();
			break;
		case R.id.saverela:
			updateLocation();
			break;
		case R.id.deleterela:

			// 默认地址不可以删除
			if (defaultid == 1) {
				Toast.makeText(this, "默认地址不可以删除", 0).show();
			} else {
				deleteLocation();
			}

			break;
		case R.id.defaultbutton:
			// 默认地址不可以删除
			if (defaultid == 1) {
				Toast.makeText(this, "默认值不可以修改，可以修改其他为默认值替换当前,", 0).show();
				return;
			}
			if (!status) {
				defaultbutton.setImageDrawable(getResources().getDrawable(
						R.drawable.default_on));
				status = !status;
			} else {
				defaultbutton.setImageDrawable(getResources().getDrawable(
						R.drawable.default_off));
				status = !status;
			}
			break;

		default:
			break;
		}

	}

	// 更新地址
	public void updateLocation() {
		String detailAddr = detailtext.getText().toString();
		String phone = phonetext.getText().toString();
		String zipCode = ziptext.getText().toString().trim();
		String contactName = nametext.getText().toString().trim();
		AddressQueryModel addressQueryModel = new AddressQueryModel();
		addressQueryModel.setId(id);
		addressQueryModel.setMobile(phone);
		addressQueryModel.setPostal_code(zipCode);
		addressQueryModel.setDetailinfo(detailAddr);
		addressQueryModel.setContactname(contactName);
		addressQueryModel.setUid(uid);
		if (status) {
			addressQueryModel.setDefault(1);
		} else {
			addressQueryModel.setDefault(0);
		}
		addressQueryPresenter.update(addressQueryModel);
	}

	// 删除地址
	public void deleteLocation() {
		AddressQueryModel model = new AddressQueryModel();
		model.setId(id);
		model.setUid(uid);
		addressQueryPresenter.delete(model);
	}

	// // 设置默认地址
	// public void setdefaultLocation() {
	// addressQueryPresenter.setDefaultAddr(uid, id);
	//
	// }

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity.getData().toString().equals("删除成功")) {
			ByAlert.alert(entity.getData().toString());
			setResult(102);
			finish();
		}
		if (entity.getData().toString().equals("修改成功")) {

			ByAlert.alert(entity.getData().toString());
			setResult(102);
			finish();
		}

	}

}
